<?php

namespace App\Http\Controllers\Home;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Model\Adv;
use App\Model\Category;
use App\Model\Comments;
use App\Model\Content;
use App\Model\Like;
use App\Model\User;
use App\Server\JwtServer;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Validator;

class HomeController extends Controller
{
  // 获取分类列表
  public function getCateList()
  {
    $data = Category::orderBy('sort', 'asc')->get();

    function rec($arr, $pid)
    {
      $temp = [];
      foreach ($arr as $v) {
        if ($v['pid'] === $pid) {
          if (rec($arr, $v['id'])) {
            $v['children'] = rec($arr, $v['id']);
          }
          $temp[] = $v;
        }
      }
      return $temp;
    }

    $data = rec($data, 0);

    return response()->json($data);
  }

  // 获取轮播图列表
  public function getSwiperList()
  {
    $data = Content::where('status', '2')->get();
    return response()->json($data);
  }

  // 获取广告列表
  public function getAdvList()
  {
    $advcontent = [];
    $advlist = Adv::where('name', 'imgbox')->get();
    foreach ($advlist as $value) {
      foreach ($value->content as $v) {
        $advcontent = $v->path;
      }
    }
    return response()->json($advcontent);
  }
}
